package com.china08.yxyapi.api.manage.edu.platform.statistics.resource;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResPhaseRepository;
import com.china08.yxyapi.service.yxyresource.statistics.StatisticsEachWeikeService;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 数据统计 数据总览 各学段资源数
 * 
 */
@CrossOrigin
@RestController
public class StatisticsEachWeiKeController extends YxyApiBaseController{
	@Autowired
	private  StatisticsEachWeikeService statisticsEachWeikeService;
	@Autowired
	private YxyResPhaseRepository yxyResPhaseRepository;
	@ApiOperation(value = "资源数据总览 各学段微课数")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/eachweike/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response){
        RespModel model = new RespModel();
        Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
        String districtCode=null;
        if (schoolBureauLevel==0) {//区
        	districtCode=getProfile(request, response).getSchoolAreaIdDistrict();
		}else if(schoolBureauLevel==1){//市
			districtCode=getProfile(request, response).getSchoolAreaIdCity();
		}else if(schoolBureauLevel==2){//省
			districtCode=getProfile(request, response).getSchoolAreaIdProvince();
		}
        
        Map<String,Object> map=statisticsEachWeikeService.statisticsResourceCount(districtCode,schoolBureauLevel);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType2 = null;
		boolQueryBuilder.mustNot(builderType2);
		
		YxyResPhase yxyResPhase=null;
		
		List<Object[]> objectsLst=new ArrayList<Object[]>();
		DecimalFormat df = new DecimalFormat("######0.00");
		Long zong=0L;
		 long long1=0;
		 Object[] obs=null;
		 /*for (Map.Entry<String,Object> entry1 : map.entrySet()) { 
			 zong+=Long.decode(entry1.getValue().toString());
		 }*/
		 for (String key : map.keySet()) {
			 zong+= Long.parseLong(map.get(key).toString());
			   System.out.println("key= "+ key + " and value= " + map.get(key));
	     }
		 
        for (String key : map.keySet()) { 
        	obs=new Object[2];
        	//1.将分组后的map集合获取到key值也就是id值，然后根据id获取到字段名称
        	yxyResPhase=yxyResPhaseRepository.findById(key);
        	if (yxyResPhase!=null) {
        		obs[0]=yxyResPhase.getName();
        		long1=Long.decode(map.get(key).toString());
            	// 将double类型的数据保存到小数点的后两位(计算的是每个学段占总共的百分比)
                obs[1]=Double.parseDouble(df.format((double)long1));
                objectsLst.add(obs);
			}
        } 
        
        String[] phaseNameList=new String[]{"幼儿园", "小学", "初中","高中", "大学"};
        List<Object[]> subList=new ArrayList<Object[]>();
               if (objectsLst.size()>0) {
                	Object[] obj=null;
                	for (String subjectName : phaseNameList) {
                		
                		for (Object[] objects : objectsLst) {
            				if (subjectName.equals(objects[0])) {
            					obj=new Object[2];
            					obj[0]=objects[0];
            					obj[1]=objects[1];
            					subList.add(obj);
            				}
            			}
        			}
        			
        		}
               
               //将新组合的list里的学段名称值连接在一起
               String phaseName="";
               for (Object[] objects : subList) {
            	   phaseName+=objects[0]+",";
			   }
                if (objectsLst.size()>subList.size()) {
                	for (Object[] countLstObj : objectsLst) {
                		    
                		    //搜索字符串中是否包含数组里的元素
            				if (phaseName.indexOf(countLstObj[0].toString())==-1) {
            					subList.add(countLstObj);
							}
        			}
        		}
        
        model.objectsLst=subList;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
    	//字段名和每个学段对应的数量
    	public List<Object[]> objectsLst;
    }
}
